package com.mytx.mapper;

import com.mytx.pojo.Donation;
import com.mytx.pojo.DonationExample;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.ResultMap;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

public interface DonationMapper {


    /**
     * 查询自己是否有有效的 （尚未转发的）历史捐赠

     */
    Donation selectMyValidHistoryDonation( @Param("uId")  String uId, @Param("topicId") String topicId,@Param("type") Integer type);


    /**
     * 查看自己是否对某人有过历史捐赠
     * @param uId   捐赠人id
     * @param targetUid   被捐赠人id
     * @return
     */
    Donation selectMyHistoryDonationToPerson( @Param("uId")  String uId, @Param("targetUid") String targetUid);

    @Select(" UPDATE donation  SET amount = amount + #{amount},updated= #{updated} WHERE id = #{donationId};SELECT amount FROM donation WHERE id = #{donationId}")
    BigDecimal incrementDonationAmount(@Param("donationId") String donationId, @Param("amount") BigDecimal amount, @Param("updated")Date updated);

    /**
     * 查询可以转发的捐赠
     *
     * @param oId
     * @param startRow
     * @param pageSize
     * @return
     */
    @ResultMap("CustomResultMapV3")
    @Select("select id,m_id,amount,parent_id from donation where o_id =#{oId} and status <=1   order by created limit #{startRow},#{pageSize} ")//status=1表示该捐赠未转发，可拆回 ，=0 表示未转发 且不可拆回
    List<Donation>selectCanForwardDonations( @Param("oId") String oId, @Param("startRow")int startRow,@Param("pageSize") int pageSize);

    /**
     * 查询发出的捐赠 记录
     *
     * @param uid
     * @return
     */
    List<Donation> querySentDonations(String uid);

    /**
     * 查询个人收到的 捐赠 记录
     *
     * @param uid
     * @return
     */
    List<Donation> queryReceivedDonations(String uid);

    /**
     * 查询个人收到的 捐赠 记录
     *
     * @param uid
     * @return
     */
    List<Donation> queryReceivedDonations_V2(String uid);

    /**
     * 根据主题id，查询出该主题收到的捐赠 记录
     *
     * @param topicId
     * @return
     */
    List<Donation> queryTopicDonations(String topicId);

    /**
     * 根据 转赠 id 查询出该笔转赠下包含的 捐赠
     * 按捐赠时间降序
     *
     * @param forwardId
     * @return
     */
    List<Donation> queryForwardDonations(String forwardId);

    /**
     * 根据 转赠 id 查询出该笔转赠下包含的 捐赠
     * 按捐赠金额降序排列
     *
     * @param topicId
     * @return
     */
    List<Donation> queryTopicDonationsOrderByAmount(String topicId);


    // 查询捐出的总金额
    @Select(" select SUM(amount)from donation where m_id = #{uId}")
    Double getSentTotalAmount(String uId);


    /**
     * 查询 用户 收到捐款的总金额
     *
     * @param tId
     * @return
     */
    @Select(" select SUM(amount) from donation where t_id = #{tId}")
    BigDecimal getReceivedTotalAmount(String tId);

    /**
     * 查询 主题 收到捐赠的总金额
     *
     * @param topicId
     * @return
     */
    @Select(" select SUM(amount) from donation where o_id = #{topicId}")
    Double getTopicTotalAmount(String topicId);

    /**
     * 查询 用户对某主题捐赠总额
     */
    @Select(" select SUM(amount)from donation where m_id = #{uId}and o_id = #{topicId} ")
    Double queryUserDonationToTopic(@Param("uId") String uId, @Param("topicId") String topicId);


    /**
     * 废弃，PageInfo 里面有total 信息
     *
     * @param uId
     * @return
     */
    @Deprecated
    @Select(" select count(*) from  donation where t_id = #{ uId}")
    Integer getReceivedTotalCount(String uId);

    int countByExample(DonationExample example);

    int deleteByExample(DonationExample example);

    int deleteByPrimaryKey(String id);

    int insert(Donation record);

    int insertSelective(Donation record);

    List<Donation> selectByExample(DonationExample example);

    Donation selectByPrimaryKey(String id);

    int updateByExampleSelective(@Param("record") Donation record, @Param("example") DonationExample example);

    int updateByExample(@Param("record") Donation record, @Param("example") DonationExample example);

    int updateByPrimaryKeySelective(Donation record);

    int updateByPrimaryKey(Donation record);

}